package tech.aistar.controller;

import com.alibaba.fastjson.JSON;
import tech.aistar.model.entity.User;
import tech.aistar.model.vo.Result;
import tech.aistar.service.IUserService;
import tech.aistar.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/4/23 1:35 下午
 */
@WebServlet(urlPatterns = "/user/*")
public class UserController extends HttpServlet {
    private IUserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();

        switch (pathInfo){
            case "/login":
                resp.setContentType("application/json;charset=utf-8");
                PrintWriter out = resp.getWriter();
                //post请求进行中文乱码处理
                req.setCharacterEncoding("utf-8");

                //根据json数据的key
                String username = req.getParameter("username");
                String password = req.getParameter("password");

                System.out.println("username"+username);
                System.out.println("password"+password);

                User user = userService.login(username);
                if(user!=null){

                    //判断密码是否ok
                    if(user.getPassword().equals(password)){

                        System.out.println(user.getPassword()+"======>");

                        //重定向到商品的首页
                        //将user放入到session作用域
                        req.getSession().setAttribute("user",user);
                        //resp.sendRedirect("/mac/product/index");

                        Result result = new Result("200","登录成功",null);
                        //转换成json字符串 - fastjson
                        String jsonStr = JSON.toJSONString(result);

                        out.print(jsonStr);

                    }else{

                        System.out.println("user==="+user);
                        //密码错误
                        Result result = new Result("500","密码错误",null);
                        //转换成json字符串 - fastjson
                        String jsonStr = JSON.toJSONString(result);

                        out.flush();

                        out.print(jsonStr);

                    }

                }else{
                    //用户不存在...
                    Result result = new Result("404","用户不存在",null);
                    //转换成json字符串 - fastjson
                    String jsonStr = JSON.toJSONString(result);

                    out.print(jsonStr);

                }
                break;
            case "/exit":
                //清空session
                req.getSession().invalidate();
                resp.sendRedirect("/mac/product/index");
                break;
        }



    }
}
